<template>
  <div class="layout-padding prod-groud">
    <div class="layout-padding-auto layout-padding-view">
      <el-row>
        <div class="mb8" style="width: 100%">
          <el-button icon="folder-add" type="primary" class="ml10" @click="openFormFn"
            >
            {{ $t('common.addBtn') }}
          </el-button>
          <el-button plain :disabled="multiple" icon="Delete" type="primary"
            @click="handleDelete(selectObjs)">
            {{ $t('common.delBtn') }}
          </el-button>
          <right-toolbar v-model:showSearch="showSearch" :export="'bizs_ProdGroupReference_export'"
            @exportExcel="exportExcel" class="ml10 mr20" style="float: right;" @queryTable="getDataList"></right-toolbar>
        </div>
      </el-row>
      <el-table :data="state.dataList" v-loading="state.loading" border :cell-style="tableStyle.cellStyle"
        :header-cell-style="tableStyle.headerCellStyle" @selection-change="selectionChangHandle"
        @sort-change="sortChangeHandle">
        <el-table-column type="selection" width="40" />
        <!--<el-table-column type="index" label="#" width="40" />-->
        <el-table-column prop="shopId" :label="$t('ProdGroupReference.shopId')" show-overflow-tooltip />
        <!-- <el-table-column prop="groupId" :label="$t('ProdGroupReference.groupId')" show-overflow-tooltip /> -->
        <el-table-column prop="prodId" :label="$t('ProdGroupReference.prodId')" show-overflow-tooltip />
        <el-table-column prop="prod.name" :label="$t('Order.prodName')" show-overflow-tooltip />
        <el-table-column prop="prod.pic" :label="$t('Order.propPic')" show-overflow-tooltip>
          <template #default="{ row }">
            <el-image @click="clickImg(row.prod.pic)" style="width: 100px; height: 100px" :src="$getFileUri(row.prod.pic)"
              fit="scale-down" />
          </template>
        </el-table-column>
        <el-table-column prop="state" :label="$t('ProdGroupReference.state')" show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="common_state_enable" :value="scope.row.state"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column :label="$t('common.action')" width="150">
          <template #default="scope">
            <!-- <el-button icon="edit-pen" text type="primary" v-auth="'bizs_ProdGroupReference_edit'"
              @click="formDialogRef.openDialog(scope.row.id)">{{ $t('common.editBtn') }}</el-button> -->
            <el-button icon="delete" text type="primary" v-auth="'bizs_ProdGroupReference_del'"
              @click="handleDelete([scope.row.id])">{{ $t('common.delBtn') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
    </div>

    <!-- 编辑、新增  -->
    <form-dialog ref="formDialogRef" @refresh="getDataList(false)" />
    <el-image-viewer :teleported="true" v-if="imgViewVisible" @close="imgViewVisible = false"
      :url-list="[$getFileUri(imageUrl)]" />
  </div>
</template>

<script setup lang="ts" name="systemProdGroupReference">
import { BasicTableProps, useTable } from "/@/hooks/table";
import { useMessage, useMessageBox } from "/@/hooks/message";
import { getPage, delObjs } from "/@/api/bizs/ProdGroupReference";

// 定义i18n
import { useI18n } from 'vue-i18n';
const { t } = useI18n();

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));

// 定义查询字典
import { useDict } from '/@/hooks/dict';

const { common_state_enable } = useDict('common_state_enable')
// 定义变量内容
const formDialogRef = ref()
// 搜索变量
const queryRef = ref()
const showSearch = ref(true)
// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

const imageUrl = ref<string>("")
const imgViewVisible = ref<boolean>(false)
const clickImg = (pic: string) => {
  imageUrl.value = pic
  imgViewVisible.value = true
}

const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {},
  pageList: getPage
})
const currentTabelItem = inject("currentTabelItem")
const openFormFn = () => {

  if (currentTabelItem.value && currentTabelItem.value.id) {
    formDialogRef.value.openDialog()
    formDialogRef.value.setGroupIdFn(currentTabelItem.value.id)
  } else {
    useMessage().error("请选择左侧分组")
  }



}

//  table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  downBlobFile,
  tableStyle
} = useTable(state)

// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  queryRef.value?.resetFields()
  // 清空多选
  selectObjs.value = []
  getDataList()
}

// 导出excel
const exportExcel = () => {
  downBlobFile('/bizs/ProdGroupReference/export', Object.assign(state.queryForm, { ids: selectObjs }), 'ProdGroupReference.xlsx')
}

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
  selectObjs.value = objs.map(({ id }) => id);
  multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
  try {
    await useMessageBox().confirm(t('common.delConfirmText'));
  } catch {
    return;
  }

  try {
    await delObjs(ids);
    getDataList();
    useMessage().success(t('common.delSuccessText'));
  } catch (err: any) {
    useMessage().error(err.msg);
  }
};

defineExpose({
  getDataList,
  state
})
</script>

<style lang="scss" scoped>
.prod-groud {
  margin: 0;
  padding: 0 !important;
  position: static !important;
}
</style>
